
$(".card").bind("input propertychange",function(event){
    console.log($(".card").val())
});


$(function () {
    var app = new Vue({
        el:'#app',
        data:{
            isXin:false,
            bankId:'',
            disabled:true,
            accttype:'',
            sheng:'',
            shi:'',
            xian:'',
            thpinfo:'',
        },
        mounted:function(){
            let _this = this
            var mobileSelect = new MobileSelect({
                trigger: '#trigger',
                title: '省市区',
                wheels: [
                    {data:city}
                ],
                keyMap: {
                    id:'areaId',
                    value: 'name',
                    childs :'sub'
                },
                callback:function(indexArr, data){
                    console.log(data[0].areaId);
                    console.log(data[1].areaId);
                    console.log(data[2].areaId);
                    _this.sheng = data[0].areaId
                    _this.shi= data[1].areaId
                    _this.xian = data[2].areaId
                }
            })
            var mobileSelect2 = new MobileSelect({
                trigger: '#trigger2',
                title: '银行',
                wheels: [
                    {data: [
                            {
                                id:1,
                                name:'储蓄卡'
                            }
                        ]},
                ],
                keyMap: {
                    id:'areaId',
                    value: 'name',
                    childs :'sub'
                },
                callback:function(indexArr, data){
                    console.log(data);
                    if(data[0].id == 2){
                        _this.isXin = true
                        _this.accttype = '02'
                    }else {
                        _this.isXin = false
                        _this.accttype = '00'
                    }
                }
            })

            this.$http.get(SURL + '/api/xlsbanks/getAllBank').then(res => {
                var mobileSelect1 = new MobileSelect({
                    trigger: '#trigger1',
                    title: '银行',
                    wheels: [
                        {data:res.body.data},
                    ],
                    keyMap: {
                        id:'id',
                        value: 'bankName',
                        childs :'sub'
                    },
                    callback:function(indexArr, data){
                        _this.bankId = data[0].id
                    }
                })
            })

            this.$http.post(SURL + '/api/xlsusersbankcards/toAddBankcards',{userId:localStorage.getItem('userId')},{emulateJSON: true}).then(res => {
                console.log(res);
                var msg = res.body.msg ;
                var cardId = res.body.data.cardId;
                var realName = res.body.data.realName;
                if(msg == "1"){
                    $(".names").prop("disabled",true);
                    $(".sfcard").prop("disabled",true);
                    this.$refs.name.value = realName;
                    _this.$refs.cardNum.value= cardId;
                }

            })



        },
        methods:{
            phoneVeation(){
                let _this = this
              	let phone = _this.$refs.phone.value;    //手机号
                let nume = _this.$refs.name.value;  //姓名
                let cardNum = _this.$refs.cardNum.value;     //身份证号
                let bankNum = _this.$refs.bankNum.value;     //银行卡号
                /*let creditNum = $('.creditNum').val(); //信用卡有效期
                let cvvNum = $('.cvvNum').val(); */       //信用卡码
                let accttype = _this.accttype            //银行卡类型
                let obj = {
                   // realName:nume,
                   // cardId:cardNum,
                   // accttype:accttype,
                   // accNo:bankNum,
		    type:5,
                    userPhone:localStorage.getItem('dengluphone'),
//                  cvv2:cvvNum,
//                  validdate:creditNum,
                    userId:localStorage.getItem('userId'),
                    // userId='+localStorage.getItem('userId')
                }
                console.log(accttype)
                if(accttype){
                    if(accttype = '00'){
                        if(phone && nume && cardNum && accttype && bankNum){
                            if(_this.disabled){
                                _this.disabled = false
                                this.$http.post(SURL + '/jfd/users/getCode', obj, {
                                    emulateJSON: true
                                }).then(function (res) {
                                    console.log(res.body.code)
                                    if(!res.body.code){
                                        $('.phoneVeation').html("60s")
                                        var time=parseInt($(".phoneVeation").text());
                                        setTime=setInterval(function(){
                                            if(time<=0){
                                                _this.disabled = true
                                                clearInterval(setTime);
                                                $('.phoneVeation').html("发送验证码")
                                                return;
                                            }
                                            time--;
                                            $(".phoneVeation").text(time + 's');
                                        },1000);
                                    }else {
                                        alert(res.body.msg)
                                        _this.disabled = true
                                    }
                                })
                            }
                        }else {
                            alert('请完善信息')
                        }
                    }else {
                        if(phone && nume && cardNum && accttype && bankNum && creditNum && cvvNum){
                            if(_this.disabled){
                                _this.disabled = false
                                this.$http.post(SURL + '/jfd/usersBankcards/agreeapply', obj, {
                                    emulateJSON: true
                                }).then(function (res) {
                                    console.log(res.body.code)
                                    if(!res.body.code){
                                        _this.thpinfo = res.body.thpinfo
                                        $('.phoneVeation').html("60s")
                                        var time=parseInt($(".phoneVeation").text());
                                        setTime=setInterval(function(){
                                            if(time<=0){
                                                _this.disabled = true
                                                $('.phoneVeation').html("发送验证码")
                                                return;
                                            }
                                            time--;
                                            $(".phoneVeation").text(time + 's');
                                        },1000);
                                    }else {
                                        alert(res.body.msg)
                                        _this.disabled = true
                                    }
                                })
                            }
                        }else {
                            alert('请完善信息')
                        }
                    }
                }else {
                    alert('请完善信息')
                }


            },
            submit(){
                let _this = this
                let phone = _this.$refs.phone.value;    //手机号
                let nume = _this.$refs.name.value;  //姓名
                let cardNum = _this.$refs.cardNum.value;     //身份证号
                let bankNum = _this.$refs.bankNum.value;     //银行卡号
                /*let creditNum = $('.creditNum').val();*/ //信用卡有效期
                /*let cvvNum = $('.cvvNum').val(); */       //信用卡码
                let accttype = _this.accttype            //银行卡类型
                /*if (!cvvNum){
                    cvvNum = ''
                }
                if(!creditNum){
                    creditNum = ''
                }*/
                var obj ={
                    accArea:$('.openingBank').val(),
                    provinceId:this.sheng,
                    cityId:this.shi,
                    areaId:this.xian,
                    bankId:this.bankId,
                    smscode:$('.verificationNum').val(),
                    thpinfo:this.thpinfo,

                    realName:nume,
                    cardId:cardNum,
                    accttype:accttype,
                    accNo:bankNum,
                    phone:phone,
//                  cvv2:cvvNum,
//                  validdate:creditNum,
                    userId:localStorage.getItem('userId')
                }
                if(phone && nume && cardNum && bankNum && accttype){
                    this.$http.post(SURL + '/api/xlsusersbankcards/agreeconfirm', obj, {
                        emulateJSON: true
                    }).then(function (res) {
                        console.log(res)
                        if(!res.body.code){
                            location.href = '../../tmp/bankCard.html'
//                            location.href = '../../tmp/personal/myorder.html?ling=1'
                        }else {
                            alert(res.body.msg)
                        }
                    })
                }else {
                    alert('请填写完整信息')
                }

                // this.$http.post('/someUrl', {params}, {emulateJSON: true}).then(function (res) {
                //     console.log(res);
                // });
            },
            backListTo(){
                location.href = '../../tmp/bankCard.html'
            }
        }
    })
})
